﻿package igz.fleaxy.ui.form.select
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import igz.fleaxy.ui.text.LabelUi;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * Elemento que contiene una etiqueta de texto y un valor para ser insertado en un SelectUi
	 * 
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class SelectItemUi extends Sprite {

		private var _Settings : Object = {
			 "bgColor" : 0xCCCCCC
			,"bgAlpha" : 1
			,"bgRound" : [0, 0, 0, 0]
			,"bgBorder" : 0x000000
			,"bgBorderThickness" : 1
		};
		
		private var _SelectUi:SelectUi;
		
		private var _Bg:Shape;
		
		private var _Label:String;
		public function get $Label():String {
			return _Label;
		}
		
		private var _Value:String;
		public function get $Value():String	{
			return _Value;
		}
		
		private var _LabelUi : LabelUi;
		public function get $LabelUi():LabelUi	{
			return _LabelUi;
		}
		protected var _Selected:Boolean;
		public function get $Selected():Boolean {
			return _Selected;
		}
		public function set $Selected(p_selected:Boolean ):void {
			_Selected = p_selected;
			if ( _Selected ) {
				_Select();
			}
			else {
				_Unselect();
			}
		}

		/**
		 * Crea el elemento item para un SelectUi
		 * 
		 * @param	p_selectUi SelectUi al que se le añade este item
		 * @param	p_settings 	_Settings : Object = {
								"bgColor" : 0xCCCCCC
								,"bgAlpha" : 1
								,"bgRound" : [0, 0, 0, 0]
								,"bgBorder" : 0x000000
								,"bgBorderThickness" : 1
							};
		 */
		public function SelectItemUi( p_selectUi:SelectUi, p_settings:Object ) {
			ObjectUtil.$Merge( p_settings, _Settings );
			
			_SelectUi = p_selectUi;
			
			_Bg = new Shape();
			_Bg.visible = false;
			addChild( _Bg );
			
			_LabelUi = new LabelUi();
			addChild( _LabelUi );
			
			
			
			mouseEnabled = true;
			tabEnabled = false;
			buttonMode = true;
			useHandCursor = true;
			
			
			addEventListener(MouseEvent.MOUSE_OVER, _Item_OnMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, _Item_OnMouseOut);
			addEventListener(MouseEvent.MOUSE_DOWN, _Item_OnMouseDown);
		}
		
		/**
		 *  Carga el texto de la etiqueta y su valor
		 * 
		 * @param	p_label Etiqueta (se muestra)
		 * @param	p_value Valor (lo que vale)
		 */
		public function $Load ( p_label:String, p_value:String ) : void {
			_Label =  p_label;
			_Value = p_value;
			
			_LabelUi.text = _Label;
		}
		
		/**
		 * Se encarga de dibujar el item teniendo en cuenta el ancho y el estio
		 * 
		 * @param	p_width ancho al que se debe ajustar
		 * @param	p_styleName estilo para dibujar
		 */
		public function $Draw (p_width:Number, p_styleName:String) : void {
			_LabelUi.$SetFixWidth( p_width );
			_LabelUi.$StyleName = p_styleName;
			
			var round : Array = _Settings["bgRound"] as Array;
			var marginBorder : Number = _Settings["bgBorderThickness"] / 2;
			_Bg.graphics.clear();
			_Bg.graphics.beginFill(_Settings["bgColor"], _Settings["bgAlpha"]);
			_Bg.graphics.lineStyle(_Settings["bgBorderThickness"], _Settings["bgBorder"]);
			_Bg.graphics.drawRoundRectComplex(marginBorder
											, marginBorder
											, p_width - _Settings["bgBorderThickness"]
											, _LabelUi.height - _Settings["bgBorderThickness"]
											, round[0], round[1], round[2], round[3]
											);
			_Bg.graphics.endFill();
		}
		
		private function _Item_OnMouseOver ( p_mouseEvent:MouseEvent ) : void {
			if ( !_Selected ) {
				_Select();
			}
		}
		
		private function _Item_OnMouseOut ( p_mouseEvent:MouseEvent ) : void {
			if ( !_Selected ) {
				_Unselect();
			}
		}
		
		private function _Item_OnMouseDown ( p_mouseEvent:MouseEvent ) : void {
			_SelectUi.$SelectItem( this );
		}

		protected function _Select():void {
			_Bg.visible = true;
		}

		protected function _Unselect():void {
			_Bg.visible = false;
		}

		public function $Enable():void {
			mouseEnabled = true;
			this.filters = [];
		}

		public function $Disable():void {
			mouseEnabled = false;
			this.filters = [new BlurFilter(6,6)];
		}
	}

}